/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import org.apache.commons.beanutils.BeanComparator;
import org.xptracker.domain.Domain;
import org.xptracker.domain.Queue;
import org.xptracker.domain.State;
import org.xptracker.domain.Story;
import org.xptracker.persistence.PersistenceManager;
import org.xptracker.services.StoryState;

import java.util.Collections;
import java.util.List;
import java.util.SortedMap;

public class StateSource implements StorySource {
  static final String ID_PREFIX = "st_";
  private final State state;

  public StateSource(State state) {
    this.state = state;
  }

  public StateSource(String srcId) {
    this.state = State.valueOf(srcId.substring(ID_PREFIX.length()));
  }

  public List<Story> getStories(Domain domain) {
    List<Story> stories = domain.listStoriesByState(state);
    //noinspection unchecked
    Collections.sort(stories, Collections.reverseOrder(new BeanComparator("severity")));
    return stories;
  }

  public String getId() {
    return ID_PREFIX + state;
  }

  public static boolean isStateSource(String srcId) {
    return srcId.startsWith(ID_PREFIX);
  }

  public String getName() {
    return state.getDescription();
  }

  public boolean isReorderable() {
    return false;
  }

  public boolean isAlive(Domain context) {
    return true;
  }

  public void addStory(Domain domain, final PersistenceManager.UpdateReceiver receiver, final QueueNotifier queueNotifier,
                       final Story story) {
    final Queue queue = story.getQueue();

    StoryState.setNextState(new StoryState.StoryStateVisitor() {
      public void setCurrentUser() {
        //Noop
      }

      public void removeOwner() {
        story.removeOwner();
      }

      public void addCurrentUserAsTracker() {
        //Noop
      }

      public void removeFromQueue(String reason) {
        if (queue != null) {
          queue.remove(story);
          receiver.update(queue);
          story.setQueue(null);
          queueNotifier.storyRemoved(queue, story, "Moved to state " + state.getDescription() + ".");
        }
      }
    }, story, state, true);

    receiver.update(story);
  }

  public void changeOrder(Domain domain, PersistenceManager.UpdateReceiver receiver, SortedMap<Integer, Story> reorderings) {
    throw new UnsupportedOperationException("Story state sources can't be ordered.");
  }

  public String getSourceType() {
    return "States";
  }

  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    StateSource that = (StateSource) o;

    if (state != that.state) return false;

    return true;
  }

  public int hashCode() {
    return state.hashCode();
  }
}
